﻿namespace RabbitDownload.Helper.Currency
{
    internal class SelenumHleper
    {
        /*

            请注意 你首先要安装 Selenium.WebDriver  然后安装Selenium.Firefox.WebDriver
             这两个缺一不可，否则会报错。
        public static IWebDriver driver = new ChromeDriver();

        /// <summary>
        /// 要抓取的链接
        /// </summary>
        /// <param name="GrabUrl"></param>
        /// <returns></returns>
        public string GetHtml(string GrabUrl, string IndexXpath, int TimeOut = 10)
        {
            string Shtml = "";
            try
            {
                driver.Manage().Window.Minimize();//设置最小化，不影响用户体验。
                driver.Manage().Timeouts();
                driver.Navigate().GoToUrl(GrabUrl);

                WebDriverWait DriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                Shtml = DriverWait.Until<string>((d) =>
                {
                    try
                    {
                        int Sleep = 10;

                        while (Sleep-- > 0 && d.FindElements(By.XPath(IndexXpath)).Count <= 0)
                        {
                            Console.WriteLine("等待元素加载，距离强制结束还有{0}秒。", Sleep);
                            Thread.Sleep(1000);
                        }
                        Console.WriteLine("找到数据数量{0}", d.FindElements(By.XPath(IndexXpath)).Count);
                        return d.PageSource;
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        return "";
                    }
                });

                //  Shtml = driver.PageSource;
                driver.Navigate().GoToUrl("about:blank");
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }

            return Shtml;
        }

        /// <summary>
        /// 未完成封装的获取cookies方法 请注意
        /// </summary>
        /// <param name="GrabUrl"></param>
        /// <returns></returns>
        public string GetCookie(string GrabUrl)
        {
            foreach (Cookie SingleCookie in driver.Manage().Cookies.AllCookies)
            {
                Console.WriteLine(SingleCookie.Domain);
                Console.WriteLine(SingleCookie.Expiry);
                Console.WriteLine(SingleCookie.Name);
                Console.WriteLine(SingleCookie.Secure);
                Console.WriteLine(SingleCookie.Value);
                Console.WriteLine(SingleCookie.Path);
            }
            return "";
        }
         */
    }
}